package org.wyn.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.wyn.common.ResultType;
import org.wyn.mongodb.common.ConnectionPool;

import com.mongodb.DB;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

/**
 * 用来测试的action
 * @author wyn
 *
 */
@Namespace("/test")
public class TestAction extends WynActionSupport{
	private static final long serialVersionUID = 1L;

	@Action(value="test",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "test.ftl")})
	public String test(){
		return SUCCESS;
	}
	/**
	 * 测试 去上传文件页面
	 * @return
	 */
	@Action(value="goFileUpload",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "fileUpload.ftl")})
	public String goFileUpload(){
		return SUCCESS;
	}
	/**
	 * 上传文件
	 * @return
	 */
	@Action(value="fileUpload",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "fileUpload.ftl")})
	public String fileUpload(){
		System.out.println(userImage);
		System.out.println(userImage.getName());
		System.out.println(userImageFileName);
		System.out.println(userImageContentType);
		DB db = ConnectionPool.getDB();
		GridFS gFs = new GridFS(db);
		try {
			GridFSInputFile gfsif = gFs.createFile(userImage);
			gfsif.setContentType(userImageContentType);
			gfsif.setFilename(userImageFileName);
			System.out.println(gfsif.getId().getClass());
			gfsif.save();
			System.out.println(gfsif.getId());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 显示图片页面
	 * @return
	 */
	@Action(value="showImage",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "showImage.ftl")})
	public String showImage(){
		return SUCCESS;
	}
	/**
	 * 下载图片
	 * @return
	 */
	@Action(value="loadImage",results = {
			@Result(name = SUCCESS, type = ResultType.STREAM,params={"inputName","inputStream"})
			})
	public String loadImage(){
		DB db = ConnectionPool.getDB();
		GridFS gFs = new GridFS(db);
		GridFSDBFile file = gFs.findOne("abc");
		//GridFSDBFile file = gFs.findOne("Desktop.ini");
		System.out.println(file.getId());
		InputStream is = file.getInputStream();
		inputStream = is;
		return SUCCESS;
	}
	private InputStream inputStream;
	private File userImage;
	
	private String userImageContentType;
	
	private String userImageFileName;

	public File getUserImage() {
		return userImage;
	}
	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}
	public String getUserImageContentType() {
		return userImageContentType;
	}
	public void setUserImageContentType(String userImageContentType) {
		this.userImageContentType = userImageContentType;
	}
	public String getUserImageFileName() {
		return userImageFileName;
	}
	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
}
